from flask import Flask, render_template, request,escape
import weather
from log_sys import log_request
from data_hn import data_hn,hn_visual,hn_pie_night,hn_pie_day
#可视化模块
from pyecharts import options as opts
from pyecharts.charts import Bar
from jinja2 import Markup

app = Flask(__name__)

#高德天气查询返回值函数
@app.route('/all_results', methods=['POST'])
def all_results() -> 'html':
    key = 'ac1c16d9ba930c1f889b26fb9a392c2f'
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是查询的结果：'
    result_humidity = str(weather.all(key, city,output='JSON', extensions='base')['humidity'])
    result_winddirection = str(weather.all(key, city, output='JSON', extensions='base')['winddirection'])
    result_windpower = str(weather.all(key, city, output='JSON', extensions='base')['windpower'])
    result_temperature = str(weather.all(key, city, output='JSON', extensions='base')['temperature'])
    result_weather = str(weather.all(key, city, output='JSON', extensions='base')['weather'])
    result_reporttime = str(weather.all(key, city, output='JSON', extensions='base')['reporttime'])

    #日志系统代码
    results = str(weather.all(key, city, output='JSON', extensions='base'))
    log_request(request, results)

    return render_template('all_results.html',
                           the_title=title,
                           the_city=city,
                           the_color1=color1,
                           the_color2=color2,
                           humidity=result_humidity,
                           winddirection=result_winddirection,
                           windpower=result_windpower,
                           temperature=result_temperature,
                           weather=result_weather,
                           reporttime=result_reporttime,
                           )
#日志系统
@app.route('/viewlog')
def view_the_log() -> 'html':
    """display the contents of thr log file as a html table"""
    contents = []
    with open('weather.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_title=titles,
                           the_data=contents)
#登陆
@app.route('/')
@app.route('/login',methods=['GET','POST'])
def login():
    return render_template("login.html",)
#注册
@app.route('/register',methods=['GET','POST'])
def register():
    return render_template("register.html",)

#网站主页
@app.route('/homepage',methods=['GET','POST'])
def homepage():
     return render_template("homepage.html", )

#天气查询空间
@app.route('/home',methods=['GET','POST'])
def home():
    title = '欢迎来到小吴的天气查询空间！'
    return render_template("home.html",
                            the_title = title,)

#高德天气查询功能
@app.route('/search',methods=['GET','POST'])
def search_page() -> 'html':
    title = '查询城市的天气实况'
    return render_template('weather_search.html',
                           the_title=title,)
#华南的广东地区天气数据表
@app.route('/hn',methods=['GET','POST'])
def data_search() -> 'html':
    title = data_hn()[0]
    td_content = data_hn()[1]
    return render_template('data_hn.html',
                           the_title = title,
                           the_td_content = td_content)

#柱状图图表
@app.route('/bar',methods=['GET','POST'])
def index():
    return render_template("index.html")

@app.route("/barChart")
def get_bar_chart():
    c = hn_visual()
    return c.dump_options_with_quotes()

#饼图1
@app.route('/Pie1',methods=['GET','POST'])
def index2():
    return render_template("index2.html")

@app.route("/pieChart1")
def get_pie_chart1():
    p1 = hn_pie_day()
    return p1.dump_options_with_quotes()

#饼图2
@app.route('/Pie2',methods=['GET','POST'])
def index3():
    return render_template("index3.html")

@app.route("/pieChart2")
def get_pie_chart2():
    p2 = hn_pie_night()
    return p2.dump_options_with_quotes()

